#include "stdafx.h"

#include "../../../include/common/base.h" 

#ifdef WIN32
	#include <Windows.h>
#endif

 
 
 #ifdef WIN32
 
string  XSysHelper::DIC_VERSION_NAME( int version ) 
{  
	char  chrVersionName[32];
	memset(chrVersionName, 0, 32);

	sprintf(chrVersionName, "%d.%d", version >> 16, version  & 0x0ff ); 

	return chrVersionName ;
}

int  XSysHelper::CMU_VER_NAME_TO_ID( string strVersion ) 
{  
	if( strVersion.empty() ) return -1; 
	
	std::size_t found  = strVersion.find(".");
	if (found ==std::string::npos)
		return -1;

	string str_main_ver = strVersion.substr(0, found);
	int main_ver = atoi( str_main_ver.c_str() );

	string str_ss_ver = strVersion.substr( found+1, strVersion.length() - found);

	int ss_ver = atoi( str_ss_ver.c_str() );
	if( ss_ver < 10 ) ss_ver = ss_ver * 10;

	return DIC_CMU_VERSION(main_ver,ss_ver)  ;
}
#else
std::string  XSysHelper::DIC_VERSION_NAME( int version ) 
{  
	char chrBuf[64];
	memset(chrBuf, 0, 64);

	sprintf( chrBuf, "%d.%d", version >> 16,  version  & 0x0ff) ;

	return chrBuf ; 
}

int  XSysHelper::CMU_VER_NAME_TO_ID( std::string strVersion ) 
{  
	int index = strVersion.find(".");
	if( index <= 0 ) return -1;

	int main_ver = atoi(strVersion.substr( 0, index ).c_str());
	int ss_ver = atoi(strVersion.substr( index+1, strVersion.length() - index-1 ).c_str() ); 

	return DIC_CMU_VERSION(main_ver,ss_ver)  ;
}

#endif


int XSysHelper::Read_Cmu_Version()
{
	char version[16] ; 

#ifdef WIN32
	GetPrivateProfileStringA("cmu_version", "ver", "1.0", version, 16,"../sysConfig/sysCmu.ini");   
#else
	base::GetProfileString("../sysConfig/sysCmu.ini", "cmu_version", "ver", version);   
#endif
	base::my_printf("version=%s on CMU \n",version);	 
	
	int verId = CMU_VER_NAME_TO_ID( version );
	
	return verId;
}
void XSysHelper::Write_Cmu_Version(int cur_cmu_version)
{
	std::string strVer = DIC_VERSION_NAME(cur_cmu_version);
#ifdef WIN32
	WritePrivateProfileStringA("cmu_version", "ver", (char*)strVer.c_str(), "../sysConfig/sysCmu.ini");
#else
	base::WriteProfileString("../sysConfig/sysCmu.ini", "cmu_version", "ver", (char*)strVer.c_str()) ;
#endif
}


bool XSysHelper::LoadSyncModuleServerInfo(struct Server_info * pSvrInfo)
{
	if( pSvrInfo == NULL ) return false;
	memset(pSvrInfo, 0, sizeof(Server_info));

	char ip[16], port[8];

	string   strNewCfgPathName = "../sysConfig/syscfg.ini";

	if( base::is_exist_file( strNewCfgPathName.c_str() ) )
	{
#ifdef WIN32
		GetPrivateProfileStringA("cmu_sync_module_version", "ip", "127.0.0.1", ip, 16, strNewCfgPathName.c_str()); 
		GetPrivateProfileStringA("cmu_sync_module_version", "port", "1", port, 8, strNewCfgPathName.c_str());   
#else
		base::GetProfileString((char*)strNewCfgPathName.c_str(), "cmu_sync_module_version", "ip", ip);  
		base::GetProfileString((char*)strNewCfgPathName.c_str(), "cmu_sync_module_version", "port", port); 
		base::my_printf("server ipAndPort = %s:%s \n",ip,port);	
#endif
	}
	else
	{ 
#ifdef WIN32
		GetPrivateProfileStringA("cmu_sync_module_version", "ip", "127.0.0.1", ip, 16, "../sysConfig/sysCmu.ini"); 
		GetPrivateProfileStringA("cmu_sync_module_version", "port", "1", port, 8, "../sysConfig/sysCmu.ini");   
#else
		base::GetProfileString("../sysConfig/sysCmu.ini", "cmu_sync_module_version", "ip", ip);  
		base::GetProfileString("../sysConfig/sysCmu.ini", "cmu_sync_module_version", "port", port); 
		base::my_printf("server ipAndPort = %s:%s \n",ip,port);	
#endif
	}
	strcpy(pSvrInfo->ipAddr, ip);
	pSvrInfo->iPort = atoi(port);
 
	return true;
} 

string XSysHelper::LoadCmuNo()
{   
	char item_buf[1024];
	memset(item_buf, 0, 1024); 

 
	string   strCfgPathName = "../sysConfig/sysCmu.ini";
	int ret = -1;

#ifdef WIN32
	GetPrivateProfileStringA("cmu_num", "num", "", item_buf, 1024, strCfgPathName.c_str()); 
#else
	ret = base::GetProfileString((char*)strCfgPathName.c_str(),  "cmu_num" ,  "num", item_buf);
	if( ret != 0 ) return "";   
#endif 
	
  
	return item_buf; 
}

string XSysHelper::LoadCmuHostIp()
{   
	char item_buf[128];
	memset(item_buf, 0, 128); 

 
	string   strCfgPathName = "../sysConfig/sysCmu.ini";
	int ret = -1;

#ifdef WIN32
	GetPrivateProfileStringA("cmu_host_ip", "HostIp", "192.168.0.101", item_buf, 1024, strCfgPathName.c_str()); 
#else
	ret = base::GetProfileString((char*)strCfgPathName.c_str(),  "cmu_host_ip" ,  "HostIp", item_buf);
	if( ret != 0 ) return "192.168.0.101";   
#endif 
	
  
	return item_buf; 
}

bool XSysHelper::LoadDevModelServerInfo(struct Server_info * pSvrInfo)
{
	if( pSvrInfo == NULL ) return false;
	memset(pSvrInfo, 0, sizeof(Server_info));

	char ip[16], port[8];

	string   strNewCfgPathName = "../sysConfig/syscfg.ini";

	if( base::is_exist_file( strNewCfgPathName.c_str() ) )
	{
#ifdef WIN32
		GetPrivateProfileStringA("dev_model_server", "ip", "", ip, 16, strNewCfgPathName.c_str()); 
		GetPrivateProfileStringA("dev_model_server", "port", "", port, 8, strNewCfgPathName.c_str()); 
#else
		base::GetProfileString((char*)strNewCfgPathName.c_str(), "dev_model_server", "ip", ip);  
		base::GetProfileString((char*)strNewCfgPathName.c_str(), "dev_model_server", "port", port); 
#endif
	}
	else
	{
#ifdef WIN32
		GetPrivateProfileStringA("dev_model_server", "ip", "", ip, 16, "../sysConfig/sysCmu.ini"); 
		GetPrivateProfileStringA("dev_model_server", "port", "", port, 8, "../sysConfig/sysCmu.ini"); 
#else
		base::GetProfileString("../sysConfig/sysCmu.ini", "dev_model_server", "ip", ip);  
		base::GetProfileString("../sysConfig/sysCmu.ini", "dev_model_server", "port", port);  
#endif
	}
	strcpy(pSvrInfo->ipAddr, ip);
	pSvrInfo->iPort = atoi(port);
 
	return true;
}

int XSysHelper::LoadScadaSvrPort()
{ 
	char item_buf[32];
	memset(item_buf, 0, 32);

	string   strNewCfgPathName = "../sysConfig/syscfg.ini";

	if( base::is_exist_file( strNewCfgPathName.c_str() ) )
	{
#ifdef WIN32
		GetPrivateProfileStringA("cmu_scada_server", "port", "", item_buf, 32, strNewCfgPathName.c_str()); 
#else
		int ret = base::GetProfileString((char*)strNewCfgPathName.c_str(),  "cmu_scada_server" ,  "port", item_buf);
		if( ret != 0 ) {
			base::my_printf("GetProfileString  fun :  read scada svr port : error! \n");  
			return -1;
		}
#endif
	}
	else
	{
		string   strCfgPathName = "../sysConfig/sysCmu.ini";
#ifdef WIN32
		GetPrivateProfileStringA("cmu_scada_server", "port", "", item_buf, 32, strCfgPathName.c_str()); 
#else
		int ret = base::GetProfileString((char*)strCfgPathName.c_str(),  "cmu_scada_server" ,  "port", item_buf);
		if( ret != 0 ) {
			base::my_printf("GetProfileString  fun :  read scada svr port : error! \n");  
			return -1;
		}
#endif
	}

	int scadaPort = atoi(item_buf);
	if( scadaPort > 0 )
	{
		//printf("scada rtdb data svr port : %d! \n", scadaPort);  
	}
	else
	{
		//printf("scada rtdb data svr port : error! \n");  
		return -1;
	}

	return scadaPort;
}

  
bool XSysHelper::IsHost()
{	  
	std::vector<string>   aryIP;
	base::GetLocalIP2(aryIP)  ;
	if( aryIP.size() <= 0 ) {
		base::my_printf("GetLocalIP2: local ip size == 0 \n");
		return false;
	}
 
    string hostip_cfg = LoadCmuHostIp(); 
	 
    for( int i=0; i<aryIP.size(); i++ )
	{
		if( strcmp( hostip_cfg.c_str(), aryIP[i].c_str() ) == 0 )
			return true;
	}
	
	return false;
}